//
//  CXVisiterView.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/18.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit
import SnapKit

class CXVisiterView: UIView {
    //图标
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //圆圈
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //消息
    private lazy var messageLabel:UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), font: 14)
        label.numberOfLines = 0
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .Center
        return label
    }()
    //注册按钮  ---  设置为公有的 方便外面使用它
    lazy var registerBtn:UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button
    }()
    //登陆按钮
    lazy var loginBtn:UIButton = {
        let button = UIButton()
        button.setTitle("登陆", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button

    }()
    // 遮罩
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    ///  纯代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        //创建好view时将各个子控件添加上去 设置好各自的内容和约束
        setupUI()
    }
    ///  从XIB或者Storyboard创建
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
// MARK: - 对外抛一个设置icon和label信息的方法
extension CXVisiterView {
    
    func setupInfo(imageNamed:String?, message:String) {
        if let imgN = imageNamed {
            circleView.hidden = true
            iconView.image = UIImage(named: imgN)
        } else {
            //如果没有设置图片 则该页是主页 主页不必额外设置图片
            
            animate()
        }
        messageLabel.text = message
    }
}
// MARK: - 设置UI
extension CXVisiterView {
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        

    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //添加约束
        //先关掉autoresize;使用第三方框架来简化布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(220)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerBtn)
            make.width.equalTo(100)
        }
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(registerBtn)
        }
    }
}
// MARK: - 设置动画 让圆圈旋转
extension CXVisiterView {
    
    private func animate() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 10
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        //当切换界面时 不移走动画；否则界面切换回来时 动画消失
        animation.removedOnCompletion = false
        circleView.layer.addAnimation(animation, forKey: "selfrotation")
    }
}
